package com.romadan.gui.controller;

import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import com.romadan.base.ActionsController;
import com.romadan.core.linearanalys.logic.LParameters;
import com.romadan.general.settings.Diagram;
import com.romadan.general.settings.PConstants;
import com.romadan.general.settings.ProjectState;
import com.romadan.gui.controller.impl.DiagramWindow;
import com.romadan.gui.controller.impl.ResultLinearAnalysisTabController;

public abstract class AbstractController {
	protected static ProjectState prjState = new ProjectState();
	protected static ActionsController actions = new ActionsController();
	protected static ResultLinearAnalysisTabController resultTab;
	protected static DiagramWindow diagramWindow;
	protected static List<LParameters> lParameters;

	protected static TextArea console;
	protected static Tab resultLinAnalysisTab;
	protected static Tab nonlinAnalysisTab;
	public static Stage stage;
	
	public static List<LParameters> getLinearParameters() {
		return lParameters;
	}

	protected final void info(String message) {
		StringBuilder sb = new StringBuilder(10000);
		sb.append("[");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		sb.append(sdf.format(new Date()).toString());
		sb.append("] ");
		sb.append(message);

		console.appendText(sb.toString() + "\n");
	}

	protected final void openDiagram(Diagram type) {
		Stage stage = new Stage();
		File fxml = new File(PConstants.PATH_TO_DIAGRAM_WINDOW);
		try {
			URL fxmlURL = fxml.toURL();
			FXMLLoader fxmlLoader = new FXMLLoader(fxmlURL);
			fxmlLoader.setController(DiagramWindow.class.newInstance());
			fxmlLoader.load();
			AnchorPane pane = fxmlLoader.getRoot();

			Scene scene = new Scene(pane);
			stage.setTitle(PConstants.NAME_DIAGRAM_WINDOW);
			stage.setScene(scene);
			stage.show();

			if (type == Diagram.DIAGRAM3X)
				diagramWindow.setVisibleForRB(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected final LineChart getLineChart(String title, String xLabel,
			String yLabel, List<Double> xValues, List<Double> yValues) {
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel(xLabel);
        yAxis.setLabel(yLabel);
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);
        
        XYChart.Series series = new XYChart.Series();
        series.setName(title);
        for (int i=0; i<yValues.size(); i++) {
        	Number x = xValues.get(i);
        	Number y = yValues.get(i);
        	series.getData().add(new XYChart.Data(x, y));
        }
        
        lineChart.getData().add(series);
        
        return lineChart;
	}
}